
package com.zhike.service.im.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 通用的业务线程实现类。<br>
 * 目的是为了提升在慢IO情况大并发时的效率且最大程度不阻塞网络IO的处理.
 * 
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 */
public class CommonConcurrentExecutor
{
	private static CommonConcurrentExecutor instance = null;
	
	private ExecutorService pool = null;
	
	public static CommonConcurrentExecutor getInstance()
	{
		if(instance == null)
			instance = new CommonConcurrentExecutor();
		return instance;
	}
	
	private CommonConcurrentExecutor()
	{
		initPool();
	}
	
	private void initPool()
	{
		// 创建一个可重用固定线程数的线程池：
		// 目前给定的最大数目是10线程，可以Chat运维过程中根据综
		// 合性能和压力情况调整此值以便达到最优状态
        pool = Executors.newFixedThreadPool(50);
	}
	
	public void execute(Runnable r)
	{
		pool.execute(r);
	}
	
	public void shutdown()
	{
		pool.shutdown();
	}
}
